using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

using Pampa.Grid.Exceptions;

namespace Pampa.Grid.Core.Actions
{
    public class ItemAction : Action
    {
        private string image;
        private List<string> parameters;
        
        public ItemAction()
        {
            parameters = new List<string>();
        }

        public ItemAction(string text, string image, string methodName) : this()
        {
            this.Text = text;
            this.Image = image;
            this.MethodName = methodName;
        }

        public string Image
        {
            get { return image; }
            set { image = value; }
        }

        public List<string> Parameters
        {
            get { return parameters; }
            set { parameters = value; }
        }

        public override void LoadXml(XmlNode node)
        {
            base.LoadXml(node);

            XmlNamespaceManager nsManager = new XmlNamespaceManager(node.OwnerDocument.NameTable);
            nsManager.AddNamespace("pampa", node.NamespaceURI);

            if (node.Attributes["Image"] != null)
                this.Image = node.Attributes["Image"].Value;

            if (node.Attributes["Text"] != null)
                this.Text = node.Attributes["Text"].Value;

            XmlNode parametersNode = node.SelectSingleNode("pampa:Parameters", nsManager);

            // Parameters
            if (parametersNode != null)
            {
                XmlNodeList fieldNodes = parametersNode.SelectNodes("pampa:Field", nsManager);

                foreach (XmlNode fieldNode in fieldNodes)
                {
                    if (fieldNode.Attributes["Id"] == null)
                        throw (new GridConfigurationException("ItemAction is not well configured. Parameter Fields must has an Id"));
                    else
                        this.Parameters.Add(fieldNode.Attributes["Id"].Value);

                }
            }
        }
    }
}
